<?php
/**
 * 文章搜索 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: search.php 538 2010-07-20 08:42:38Z lisijie86 $
*/

!defined('IN_JBLOG') && exit('Access Denied!');
$time_limit = 30;	//搜索时间间隔/秒
$search_limit = 500; //搜索结果最多显示条数
$search_cache_time = 180; //搜索缓存时间/秒

initGP(array('keyword','dosubmit','sid','type','cateid','time','timetype','orderby','ascdesc','page'));
if ( $get['sid'] ) {
	$sid = $get['sid'];
	$page = $get['page'];
}
if ( $dosubmit || $sid ) {

	if ( $sid ) {
		$sid = intval($sid);
		$page = max(1, intval($page));
		$pagesize = config('pagesize');
		$offset = ($page-1) * $pagesize;
		$url = "index.php?mod=search&sid={$sid}";
		$query = $db->query("SELECT * FROM ".tname('search')." WHERE id = '$sid' AND expiration > '".NOW."'");
		if ( ! $search = $db->fetch_array($query) ) {
			show_msg(__('搜索不存在或已过期。'));
		}
		$count = $search['count'];
		if ( $offset < $count ) {
			$search['postids'] = explode(',', $search['postids']);
			$ids = array_slice($search['postids'], $offset, $pagesize);
			query_post('blog', 0, 0, " id IN (".simplode($ids).")");
		}
		$keyword = htmlspecialchars($search['keyword'], ENT_QUOTES);
		blog('title', __('搜索：%s', $keyword));
		blog('keywords', $keyword);
		blog('description', $keyword);
	} else {
		if ( strlen($keyword) < 3 ) {
			show_msg(__('搜索关键字不能少于3个字节。'));
		} elseif ( strlen($keyword) > 50 ) {
			show_msg(__('搜索关键字不能大于50字节。'));
		} else {
			if ( $lasttime = get_cookie('last_search') ) {
				if ( NOW < $lasttime + $time_limit ) {
					show_msg(__('对不起，30秒内只能进行一次搜索。'));
				} 
			}
			set_cookie('last_search', NOW);
			$keyword = str_replace(array('%','_','|'),array('\%','\_','+'),check_str($keyword));
			$type != 'title' && $type = 'fulltext';
			$cateid = intval($cateid);
			$time != 'all' && $time = intval($time);
			$timetype != 1 && $timetype = 0;
			if ( $orderby != 'views' && $orderby != 'comments' ) { 
				$orderby = 'dateline';
			}
			$ascdesc != 'asc' && $ascdesc = 'desc';
			
			$searchtext = "$keyword|$type|$cateid|$time|$timetype|$orderby|$ascdesc";
			
			$query = $db->query("SELECT id FROM ".tname('search')." WHERE 
			searchtext = '$searchtext' AND expiration > '".NOW."'");
			if ( $index = $db->fetch_array($query) ) {
				$sid = $index['id'];
			} else {
				$wheresql = '';
				if ($type == 'title') {
					$wheresql .= " AND title LIKE '%$keyword%'";
				} else {
					$wheresql .= " AND title LIKE '%$keyword%' OR content LIKE '%$keyword%'";
				}
				if ($cateid) {
					$wheresql .= " AND cateid = '$cateid'";
				}
				if ( $time != 'all' ) {
					$sc_time = NOW - $time;
					if ($timetype == 1) {
						$wheresql .= " AND dateline < $sc_time";
					} else {
						$wheresql .= " AND dateline > $sc_time";
					}
				}
				$postids = array();
				$query = $db->query("SELECT id FROM ".tname('post')." 
				WHERE `type` = 'blog' $wheresql AND `status` = 1 ORDER BY $orderby $ascdesc LIMIT $search_limit");
				while ( $row = $db->fetch_array($query) ) {
					$postids[] = $row['id'];
				}
				$search = array(
					'keyword' => $keyword, 'searchtext' => $searchtext, 'count' => count($postids), 
					'postids' => implode(',',$postids), 'ip' => $_USER['ip'], 'expiration' => NOW + $search_cache_time,
					'dateline' => NOW
				);
				$sid = $db->insert('search', $search);
			}
			
			redirect('index.php?mod=search&sid='.$sid.'&dosubmit=1');
		}
	}
	
	template('list');

} else {

	function get_category_options($selid = 0, $type = 'blog') {
		global $_CACHE;
		$output = '';
		if ( $_CACHE['cate'][$type] ) {
			foreach ( $_CACHE['cate'][$type] as $cateid ) {
				$cate = $_CACHE['cate'][$cateid];
				$selstr = $cate['id'] == $selid ? ' selected="selected"' : '';
				$output .= '<option value="'.$cate['id'].'"'.$selstr.'>&nbsp;&nbsp;|-'.$cate['catename'].'</option>';
			}
		}
		echo $output;
	}
	
	blog('title', __('文章搜索'));
	
	template('search');
}
?>